<?php

namespace App\Http\Controllers\Admin\Cashier;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Allocation;
use App\Models\Cashier\Store;
use App\Models\Cashier\Product;

class AllocationController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_allocation' => 'active',
            'stores' => Store::orderBy('id', 'desc')->get()
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按调入调出店
            if ($request->has('store_id') and $request->store_id != '-1') {
                $query->where('store_id', $request->store_id);
            }

            //按照时间搜索
            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                $start_time = $time[0] . ' 00:00:00'; //这里单引号前面要打一空格
                $end_time = $time[1] . ' 23:59:59';
                $query->whereBetween('created_at', [$start_time, $end_time]);
            }
        };
        $allocations = Allocation::where($where)->orderBy('created_at', 'desc')->paginate(env('pageSize'));
        return view('admin.cashier.allocation.index', compact('allocations'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.allocation.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //调拨成功后，原店铺该商品的库存减掉，新店铺该商品的库存增加
        $product = Product::where('store_id', $request->in_store)->where('code', $request->code)->first();
//        return $product;
        if ($product) {
            Product::where('store_id', $request->in_store)->where('code', $request->code)
                ->increment('stock');
            Product::where('store_id', $request->out_store)->where('code', $request->code)
                ->decrement('stock');

        } else {
            return back()->with('error', '商品调拨失败，请检查调入店铺是否有该商品~');
        }

        //调拨成功后数据插入调拨表
        Allocation::create($request->all());


        return redirect(route('cashier.allocation.index'))->with('success', '商品挑拨成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $allocation = Allocation::find($id);

        return view('admin.cashier.allocation.edit', compact('allocation'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'number' => 'required|integer|max:255',
            'code' => 'required|string',
            'handle_person' => 'required|string',
        ]);

        $allocation = Allocation::find($id);
        $allocation->update($request->all());
        return redirect(route('cashier.allocation.index'))->with('success', '编辑成功~');
    }

    /**
     * ajax删除单条
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Allocation::destroy($id);
    }


    /***
     * ajax删除多条
     * @param Request $request
     */
    public function destroy_more(Request $request)
    {
        Allocation::destroy($request->checked_id);
    }
}
